package com.tanhua.server.service.impl;

import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.model.domain.Question;
import com.tanhua.server.intercepter.UserThreadLocal;
import com.tanhua.server.service.QuestionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class QuestionServiceImpl implements QuestionService {

    @DubboReference
    private QuestionApi questionApi;

    @Override
    public void addQuestion(String content) {
        //获取当前用户id
        Long userId = UserThreadLocal.getUserId();
        //调用Api查询当前用户的陌生人问题
        Question byUserId = questionApi.findByUserId(userId);
        //通过判断该用户是否存在得到:问题是否有存在
        if (byUserId == null) {
            //没有创建一个新的问题
            byUserId = new Question();
            byUserId.setUserId(userId);
            byUserId.setTxt(content);
            //调用Api再将新的问题数据添加到数据库
            questionApi.addQuestion(byUserId);
        } else {
            //存在,修改数据库信息为新的问题数据
            byUserId.setTxt(content);
            questionApi.updateQuestion(byUserId);

        }

    }
}
